package com.huanletao.spring.aop;


import com.huanletao.proxy.jdkproxy.PermissionInterceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * <p>
 *
 * </p>
 *
 * @package: com.huanletao.spring.aop
 * @Description:
 * @className: PermissionMethodInterceptor
 * @author: @NNCSKJ ADMIN
 * @copyright: Copyright (c) 2022/6/26 16:23
 * @version: V1.0
 */
public class PermissionMethodInterceptor implements MethodInterceptor {

    private static Logger logger = LoggerFactory.getLogger(PermissionInterceptor.class);

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        logger.info(getClass() + "权限检查");
        Object[] arguments = invocation.getArguments();
        logger.info("arguments  = {}", Arrays.toString(arguments));
        AccessibleObject staticPart = invocation.getStaticPart();
        Method method = invocation.getMethod();
        System.out.println("method = " + method);
        System.out.println("staticPart = " + staticPart);
        if (!arguments[0].equals("admin")) throw new RuntimeException("么有权限异常");
        Object ret = invocation.proceed();
        return ret;
    }
}
